home *** CD-ROM | disk | FTP | other *** search
/ Complete Linux / Complete Linux.iso / docs / apps / database / postgres / postgre4.z / postgre4 / src / utils / error / exc.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-08-27  |  3.3 KB  |  169 lines

  1. /*
  2.  * exc.c --
  3.  *    POSTGRES exception handling code.
  4.  *
  5.  * Note:
  6.  *    XXX this code needs improvement--check for state violations and
  7.  *    XXX reset after handling an exception.
  8.  */
  9.  
  10. #include <stdio.h>    /* XXX use own I/O routines */
  11.  
  12. #include "tmp/c.h"
  13. #include "utils/exc.h"
  14.  
  15. RcsId("$Header: /private/postgres/src/utils/error/RCS/exc.c,v 1.7 1990/09/25 16:51:41 kemnitz Exp $");
  16.  
  17. /*
  18.  * Global Variables
  19.  */
  20. static bool ExceptionHandlingEnabled = false;
  21.  
  22. String            ExcFileName = NULL;
  23. Index            ExcLineNumber = 0;
  24.  
  25. ExcFrame        *ExcCurFrameP = NULL;
  26.  
  27. static    ExcProc        *ExcUnCaughtP = NULL;
  28.  
  29. /*
  30.  * Exported Functions
  31.  */
  32.  
  33. /*
  34.  * Excection handling should be supported by the language, thus there should
  35.  * be no need to explicitly enable exception processing.
  36.  *
  37.  * This function should probably not be called, ever.  Currently it does
  38.  * almost nothing.  If there is a need for this intialization and checking.
  39.  * then this function should be converted to the new-style Enable code and
  40.  * called by all the other module Enable functions.
  41.  */
  42. void
  43. EnableExceptionHandling(on)
  44.     bool    on;
  45. {
  46.     if (on == ExceptionHandlingEnabled) {
  47.         /* XXX add logging of failed state */
  48.         exitpg(255);
  49.         /* ExitPostgres(FatalExitStatus); */
  50.     }
  51.  
  52.     if (on) {    /* initialize */
  53.         ;
  54.     } else {    /* cleanup */
  55.         ExcFileName = NULL;
  56.         ExcLineNumber = 0;
  57.         ExcCurFrameP = NULL;
  58.         ExcUnCaughtP = NULL;
  59.     }
  60.  
  61.     ExceptionHandlingEnabled = on;
  62. }
  63.  
  64. void
  65. ExcPrint(excP, detail, data, message)
  66.     Exception    *excP;
  67.     ExcDetail    detail;
  68.     ExcData        data;
  69.     ExcMessage    message;
  70. {
  71.     extern    int    errno;
  72.     extern    int    sys_nerr;
  73.     extern    char    *sys_errlist[];
  74.  
  75. #ifdef    lint
  76.     data = data;
  77. #endif
  78.  
  79.     (void) fflush(stdout);    /* In case stderr is buffered */
  80.  
  81. #if    0
  82.     if (ProgramName != NULL && *ProgramName != '\0')
  83.         (void) fprintf(stderr, "%s: ", ProgramName);
  84. #endif
  85.  
  86.     if (message != NULL)
  87.         (void) fprintf(stderr, "%s", message);
  88.     else if (excP->message != NULL)
  89.         (void) fprintf(stderr, "%s", excP->message);
  90.     else
  91. #ifdef    lint
  92.         (void) fprintf(stderr, "UNNAMED EXCEPTION 0x%lx", excP);
  93. #else
  94.         (void) fprintf(stderr, "UNNAMED EXCEPTION 0x%lx", (long)excP);
  95. #endif
  96.  
  97.     (void) fprintf(stderr, " (%ld)", detail);
  98.  
  99.     if (errno > 0 && errno < sys_nerr &&
  100.         sys_errlist[errno] != NULL && sys_errlist[errno][0] != '\0')
  101.         (void) fprintf(stderr, " [%s]", sys_errlist[errno]);
  102.     else if (errno != 0)
  103.         (void) fprintf(stderr, " [Error %d]", errno);
  104.  
  105.     (void) fprintf(stderr, "\n");
  106.  
  107.     (void) fflush(stderr);
  108. }
  109.  
  110. ExcProc *
  111. ExcGetUnCaught()
  112. {
  113.     return (ExcUnCaughtP);
  114. }
  115.  
  116. ExcProc *
  117. ExcSetUnCaught(newP)
  118.     ExcProc    *newP;
  119. {
  120.     ExcProc    *oldP = ExcUnCaughtP;
  121.  
  122.     ExcUnCaughtP = newP;
  123.  
  124.     return (oldP);
  125. }
  126.  
  127. void
  128. ExcUnCaught(excP, detail, data, message)
  129.     Exception    *excP;
  130.     ExcDetail    detail;
  131.     ExcData        data;
  132.     ExcMessage    message;
  133. {
  134.     ExcPrint(excP, detail, data, message);
  135.  
  136.     ExcAbort(excP, detail, data, message);
  137.     /*NOTREACHED*/
  138. }
  139.  
  140. void
  141. ExcRaise(excP, detail, data, message)
  142.     Exception    *excP;
  143.     ExcDetail    detail;
  144.     ExcData        data;
  145.     ExcMessage    message;
  146. {
  147.     register ExcFrame    *efp;
  148.  
  149.     efp = ExcCurFrameP;
  150.     if (efp == NULL) {
  151.         if (ExcUnCaughtP != NULL)
  152.             (*ExcUnCaughtP)(excP, detail, data, message);
  153.  
  154.         ExcUnCaught(excP, detail, data, message);
  155.         /*NOTREACHED*/
  156.     } else {
  157.         efp->id        = excP;
  158.         efp->detail    = detail;
  159.         efp->data    = data;
  160.         efp->message    = message;
  161.  
  162.         ExcCurFrameP = efp->link;
  163.  
  164.         longjmp(efp->context, 1);
  165.         /*NOTREACHED*/
  166.     }
  167.     /*NOTREACHED*/
  168. }
  169.